<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">工厂</label>
        <div class="layui-input-block">
            <select name="supplierId" id="supplierId" lay-search="">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">生成工厂合同</button>
        </div>
    </div>
</div>

</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    let contractId = $.common.getUrlParam("contractId");
    layui.use(['form'], function () {
        var form = layui.form;

        let callback = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#supplierId').append(new Option('请选择', '', true));// 下拉菜单里添加元素
                    $('#supplierId').append(new Option('生成所有工厂合同', '0'));
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.supplierName, item.id);
                        if (supplierId) {
                            if (item.id == supplierId) {
                                option.selected = true;
                            }
                        }
                        $('#supplierId').append(option);// 下拉菜单里添加元素
                    });
                } else {
                    $.common.showErrorMessage(result.message)
                    return false;
                }
            }
        };
        $.xAjax('/contractDetail/getSupplierByContractId/' + contractId, 'POST', JSON.stringify({}), callback);
        //初始化下拉框
        form.render("select");
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let supplierId = data.field.supplierId
            data.field.contractId = contractId;
            url = '/supplierContract/createByContract/'+contractId+'/'+supplierId
            let callback = {
                onSuccess: function (result) {
                    if (result.code == '0') {

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);
                    } else {
                        $.common.showErrorMessage(result.message)
                        return false;
                    }
                }
            };
            $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
            return false;
        });
    });
</script>
</body>
</html>